// pages/request/request.js
// const url = 'http://127.0.0.1:3000';
const url = 'http://192.168.0.106:3000';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downLoadPath: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getReq() {
    console.log(url)
    wx.request({
      url: url + '/getUser?id=123456',
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      data: {
        name: '哈哈哈'
      },
      dataType: 'json',
      success({
        data,
        statusCode,
        header
      }) {
        console.log(data);
      }
    })
  },
  post() {
    wx.request({
      url: url + '/getData',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
        // 'content-type': 'application/json'
      },
      data: {
        name: '哈哈哈'
      },
      dataType: 'json',
      success({
        data,
        statusCode,
        header
      }) {
        console.log(data);
      }
    })
  },
  upload() {
    let _this = this;
    wx.chooseImage({
      success: function(res) {
        let path = res.tempFilePaths[0];
        wx.uploadFile({
          url: url + '/upload',
          name: 'file',
          filePath: path,
          success({
            data,
            statusCode
          }) {
            console.log(data)
            let path = JSON.parse(data).data.path;
            path = path.replace(/\\/g,"/").replace('public','');
            _this.setData({
              downLoadPath: path
            })
            wx.showToast({
              title: '上传成功',
            })
          },
          fail() {
            wx.showToast({
              title: '上传失败',
            })
          }
        })
      },
    })
  },
  download() {
    console.log(url + this.data.downLoadPath);
    wx.downloadFile({
      url:url+this.data.downLoadPath,
      success:({tempFilePath,filePath,statusCode}) => {
        console.log(tempFilePath)
        console.log(filePath)
        wx.showToast({
          title: '下载成功',
        })
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(){
            wx.showToast({
              title: '保存成功',
            })
          }
        })
      }
    })
  }
})